package com.wngbms.netty.gatewaydemo.filter;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;

public class AddRequestHeaderFilter implements HttpRequestFilter {

    private String key;
    private String value;
    private HttpRequestFilter nextFilter;

    public AddRequestHeaderFilter() {
    }

    public AddRequestHeaderFilter(String key, String value) {
        this.key = key;
        this.value = value;
    }

    public AddRequestHeaderFilter(HttpRequestFilter nextFilter) {
        this.nextFilter = nextFilter;
    }

    @Override
    public void filter(FullHttpRequest fullHttpRequest, ChannelHandlerContext ctx) {
        fullHttpRequest.headers().set(key, value);

        if (nextFilter != null) {
            nextFilter.filter(fullHttpRequest, ctx);
        }
    }

    public void setNextFilter(HttpRequestFilter nextFilter) {
        this.nextFilter = nextFilter;
    }
}
